package cn.shierblog.spider_zhiping;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public abstract class AbstractSpider implements Spider, AutoCloseable {

    protected final String url;

    protected final ChromeDriver driver;

    public AbstractSpider(String url) {
        this.url = url;

        ChromeOptions chromeOptions = new ChromeOptions();
        //创建无Chrome无头参数
        chromeOptions.setHeadless(true);
        ChromeOptions options=new ChromeOptions();
        this.driver = new ChromeDriver(options);
    }


    @Override
    public void craw() {
        try{
            driver.get(url);
            spider();
        } catch (Exception e) {

        } finally {
            close();
        }
    }

    public abstract void spider() throws Exception;

    @Override
    public void close()  {
        this.driver.close();
    }
}
